package com.baibei.ebec.user.register;

import android.content.Context;
import android.text.TextUtils;

import com.baibei.basic.BasicPresenterImpl;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.IUserApi;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.Empty;

/**
 * 企业用户注册
 * Created by ChenRui on 2017/8/3 0003 17:24.
 */
public class RegisterEnterprisePresenterImpl extends BasicPresenterImpl<RegisterEnterpriseContract.View> implements RegisterEnterpriseContract.Presenter {

    private IUserApi mUserApi;


    public RegisterEnterprisePresenterImpl(Context context, RegisterEnterpriseContract.View view) {
        super(context, view);
        mUserApi = ApiFactory.getInstance().getUserApi();
    }


    @Override
    public void register() {
        final String mobile = mView.getMobile();
        final String password = mView.getPassword();
        final String smsCode = mView.getSmsCode();
        String legalPersonName = mView.getLegalPersonName();
        String legalPersonCertNo = mView.getLegalPersonCertNo();
        String bizLicencePicture = mView.getBizLicencePicture();


        if (TextUtils.isEmpty(password)) {
            mView.onRegisterFailed("请输入密码");
            return;
        }
        if (TextUtils.isEmpty(mobile)) {
            mView.onRegisterFailed("请输入手机号码");
            return;
        }

        // 0、获取阿里云上传参数
        // 1、获取到本地图片，上传到阿里云
        // 2、调用注册

        createObservable(mUserApi.enterpriseRegister(mobile, password, smsCode, legalPersonName, legalPersonCertNo, bizLicencePicture))
//                .flatMap(new Function<Empty, ObservableSource<UserInfo>>() {
//                    @Override
//                    public ObservableSource<UserInfo> apply(@NonNull Empty empty) throws Exception {
//                        return createObservable(mUserApi.signIn(mobile, password));
//                    }
//                })
                .subscribe(new ApiDefaultObserver<Empty>() {
                    @Override
                    protected void onError(String s) {
                        mView.onRegisterFailed(s);
                    }

                    @Override
                    protected void accept(Empty userInfo) {
//                        SessionManager.getDefault().setUser(userInfo);
                        mView.onRegisterSuccess();
                    }
                });

    }

    @Override
    public void cancel() {
        cancelRequest();
    }
}
